package com.leancloud.circle.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.aries.ui.view.title.TitleBarView;
import com.leancloud.circle.R;
import com.leancloud.circle.api.CircleApiConstant;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;

import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.basecomponent.ServiceFactory;
import tech.com.commoncore.cache.CacheData;
import tech.com.commoncore.constant.ApiConstant;
import tech.com.commoncore.entity.BaseTResp;
import tech.com.commoncore.utils.ToastUtil;


public class CommentActivity extends BaseTitleActivity {

    private EditText mEtContent;


    private long circleId;

    @Override
    public void beforeSetContentView() {
        super.beforeSetContentView();
        circleId = getIntent().getLongExtra("circleId", 0L);
        if (circleId == 0L) {
            finish();
        }
    }

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText(getResources().getString(R.string.circle_comm)).setStatusBarLightMode(false).setLeftTextDrawable(R.mipmap.back_white).setTitleMainTextColor(Color.WHITE).setRightTextColor(Color.WHITE).setRightText("发布").setRightTextColor(Color.WHITE).setOnRightTextClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }

    @Override
    public int getContentLayout() {
        return R.layout.circle_activity_comment;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mEtContent = findViewById(R.id.et_content);
    }

    private void submit() {
        String content = mEtContent.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            ToastUtil.show(getResources().getString(R.string.circle_content_hint));
            return;
        }

        showLoading(getResources().getString(R.string.circle_seeding));

        ViseHttp.POST(CircleApiConstant.ADD_COMMENT)
                .baseUrl(ApiConstant.BASE_URL)
                .addHeader("XX-Token",ServiceFactory.getInstance().getLoginService().getCurrentUser().getToken())
                .addHeader("XX-Device-Type", "android")
                .addParam("object_id", circleId + "")
                .addParam("content", content)
                .request(new ACallback<BaseTResp<String>>() {

                    @Override
                    public void onSuccess(BaseTResp<String> data) {
                        hideLoading();
                        if ((1 == data.code)) {
                            ToastUtil.show(getResources().getString(R.string.circle_commentted));
                            setResult(RESULT_OK);
                            finish();
                        } else {
                            ToastUtil.show(data.msg);
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        hideLoading();
                        ToastUtil.show(getResources().getString(R.string.circle_commentted_fail));
                    }
                });

    }


    public static Intent actionToActivity(Context context, long circleId) {
        Intent intent = new Intent(context, CommentActivity.class);
        intent.putExtra("circleId", circleId);
        return intent;
    }

}
